
#include <bur/plctypes.h>

#ifdef _DEFAULT_INCLUDES
	#include <AsDefault.h>
#endif

void _INIT ProgramInit(void)
{

}





void _CYCLIC ProgramCyclic(void)
{

	if((gAssembly.ErrorStop == 0) && (EdgeAssemblyErrorStop))
	{
		brsmemset((UDINT)&gAssembly.LoggerSegmentError, 0, sizeof(gAssembly.LoggerSegmentError));
	}

	do
	{
		StepLast = Step;
		switch(Step)
		{
		
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			// COMMAND
	
			case 0:
				if(((gAssembly.ErrorStop) && (EdgeAssemblyErrorStop == 0)) || (CmdRefresh))
				{
					CmdRefresh = 0;
					Step = 1;
				}
			break;
			
			///////////////////////////////////////////////////////////////////////////
			// wait till all messages are entered into logger
			case 1:
				FubTonWaitFirst.IN = 1;
				FubTonWaitFirst.PT = 1000;
				TON(&FubTonWaitFirst);
				if(FubTonWaitFirst.Q)
				{
					FubTonWaitFirst.IN = 0;
					
					brsmemset((UDINT)&gAssembly.LoggerSegmentError, 0, sizeof(gAssembly.LoggerSegmentError));
					brsstrcpy((UDINT)&ErrorMessage, (UDINT)&"");
					SegmentErrorFound = 0;
					
					FubGetIdent.Execute				= 0;
					FubGetLatestRecordID.Execute	= 0;
					FubGetPreviousRecordID.Execute	= 0;
					FubRead.Execute					= 0;
					FubReadObjectID.Execute			= 0;
					FubReadDescription.Execute		= 0;
					Step = 10;
				}
			break;
	
			///////////////////////////////////////////////////////////////////////////
			// open logger and read first EventID
			case 10:
				FubGetIdent.Execute	= 1;
				brsstrcpy((UDINT)&FubGetIdent.Name, (UDINT)&"$motion");
				Step = 11;
			break;
			case 11:
				if(FubGetIdent.Done)
				{
					Ident = FubGetIdent.Ident;
					FubGetIdent.Execute = 0;
					Step = 15;
				}
				else if(FubGetIdent.Error)
				{
					brsstrcpy((UDINT)&ErrorMessage, (UDINT)&"Error GetIdent");
					Step = 99;
				}
			break;
			
			case 15:
				FubGetLatestRecordID.Execute	= 1;
				FubGetLatestRecordID.Ident		= Ident;
				Step = 16;
			break;
			case 16:
				if(FubGetLatestRecordID.Done)
				{
					RecordID = FubGetLatestRecordID.RecordID;
					FubGetLatestRecordID.Execute = 0;
					Step = 30;
				}
				else if(FubGetLatestRecordID.Error)
				{
					brsstrcpy((UDINT)&ErrorMessage, (UDINT)&"Error GetLatestRecordID");
					Step = 99;
				}
			break;
			
			///////////////////////////////////////////////////////////////////////////
			// read previous EventID
			case 20:
				FubGetPreviousRecordID.Execute	= 1;
				FubGetPreviousRecordID.Ident	= Ident;
				FubGetPreviousRecordID.RecordID	= RecordID;
				Step = 21;
			break;
			case 21:
				if(FubGetPreviousRecordID.Done)
				{
					RecordID = FubGetPreviousRecordID.PrevRecordID;
					FubGetPreviousRecordID.Execute = 0;
					Step = 30;
				}
				else if(FubGetPreviousRecordID.Error)
				{
					if(FubGetPreviousRecordID.StatusID == arEVENTLOG_ERR_RECORDID_INVALID)		// Ende des Loggers erreicht
					{
						FubGetPreviousRecordID.Execute = 0;
						Step = 90;
					}
					else
					{
						brsstrcpy((UDINT)&ErrorMessage, (UDINT)&"Error GetPreviousRecordID");
						Step = 99;
					}
				}
			break;
			
			///////////////////////////////////////////////////////////////////////////
			// check if severity is "Error"
			case 30:
				FubRead.Execute		= 1;
				FubRead.Ident		= Ident;
				FubRead.RecordID	= RecordID;
				Step = 31;
			break;
			case 31:
				if(FubRead.Done)
				{
					EventID = FubRead.EventID;
					FubRead.Execute = 0;
					EventTimeStamp = FubRead.TimeStamp;

					EventIDSeverity	= (EventID >> 30) & 0x03;
					EventIDFacility	= (EventID >> 16) & 0x0FFF;
					EventIDCode		= (EventID >> 0) & 0xFFFF;

					if(EventIDSeverity == arEVENTLOG_SEVERITY_ERROR)
					{
						Step = 40;
					}
					else
					{
						Step = 20;
					}
				}
				else if(FubRead.Error)
				{
					brsstrcpy((UDINT)&ErrorMessage, (UDINT)&"Error GetPreviousRecordID");
					Step = 99;
				}
			break;
	
			///////////////////////////////////////////////////////////////////////////
			// check if "Error" has been entered by a Segment
			case 40:
				FubReadObjectID.Execute		= 1;
				FubReadObjectID.Ident		= Ident;
				FubReadObjectID.RecordID	= RecordID;
				Step = 41;
			break;
			case 41:
				if(FubReadObjectID.Done)
				{
					brsstrcpy((UDINT)&ObjectID, (UDINT)&FubReadObjectID.ObjectID);
					FubReadObjectID.Execute = 0;
					if(brsmemcmp((UDINT)&ObjectID, (UDINT)&"Seg_", 3) == 0)
					{
						Step = 50;
					}
					else
					{
						Step = 20;
					}
				}
				else if(FubReadObjectID.Error)
				{
					brsstrcpy((UDINT)&ErrorMessage, (UDINT)&"Error GetPreviousRecordID");
					Step = 99;
				}
			break;
	
			///////////////////////////////////////////////////////////////////////////
			// read and display description text if filter criterias are matching
			case 50:
				FubReadDescription.Execute			= 1;
				FubReadDescription.Ident			= Ident;
				FubReadDescription.RecordID			= RecordID;
	//			FubReadDescription.LanguageCode		= ;
	//			FubReadDescription.Qualifier		= ;
				FubReadDescription.TextBuffer		= (UDINT)&Description;
				FubReadDescription.TextBufferSize	= sizeof(Description);
				Step = 51;
			break;
			case 51:
				if(FubReadDescription.Done)
				{
					FubReadDescription.Execute = 0;
					Step = 60;
				}
				else if(FubReadDescription.Error)
				{
					brsstrcpy((UDINT)&ErrorMessage, (UDINT)&"Error GetPreviousRecordID");
					Step = 99;
				}
			break;
	
			case 60:
				brsstrcpy((UDINT)&gAssembly.LoggerSegmentError[SegmentErrorFound].ObjectID, (UDINT)&ObjectID);
				gAssembly.LoggerSegmentError[SegmentErrorFound].EventID			= EventID;
				gAssembly.LoggerSegmentError[SegmentErrorFound].EventIDSeverity	= EventIDSeverity;
				gAssembly.LoggerSegmentError[SegmentErrorFound].EventIDFacility	= EventIDFacility;
				gAssembly.LoggerSegmentError[SegmentErrorFound].EventIDCode		= EventIDCode;

				gAssembly.LoggerSegmentError[SegmentErrorFound].EventTimeStamp	= (TIME)((EventTimeStamp.sec % (24*60*60))*1000 + (EventTimeStamp.nsec / 1000000));		// convert to TIME format (ms since start of day)

				brsstrcpy((UDINT)&gAssembly.LoggerSegmentError[SegmentErrorFound].Text, (UDINT)&"");
				ttoa(gAssembly.LoggerSegmentError[SegmentErrorFound].EventTimeStamp, (UDINT)&TempString);
				brsstrcat((UDINT)&gAssembly.LoggerSegmentError[SegmentErrorFound].Text, (UDINT)&TempString);
				brsstrcat((UDINT)&gAssembly.LoggerSegmentError[SegmentErrorFound].Text, (UDINT)&" => ");

				brsstrcat((UDINT)&gAssembly.LoggerSegmentError[SegmentErrorFound].Text, (UDINT)&ObjectID);
	
				brsstrcat((UDINT)&gAssembly.LoggerSegmentError[SegmentErrorFound].Text, (UDINT)&": Error ");
				brsitoa(EventIDCode, (UDINT)&TempString);
				brsstrcat((UDINT)&gAssembly.LoggerSegmentError[SegmentErrorFound].Text, (UDINT)&TempString);
	
				brsstrcat((UDINT)&gAssembly.LoggerSegmentError[SegmentErrorFound].Text, (UDINT)&" => ");
				brsstrcat((UDINT)&gAssembly.LoggerSegmentError[SegmentErrorFound].Text, (UDINT)&Description);
				
				SegmentErrorFound++;
				if(SegmentErrorFound >= 2)
					Step = 90;
				else
					Step = 20;
			break;
			
			///////////////////////////////////////////////////////////////////////////
			case 90:
				Step = 0;
			break;
	
			///////////////////////////////////////////////////////////////////////////
			case 99:
				if(gAssembly.ErrorStop == 0)
				{
					brsstrcpy((UDINT)&ErrorMessage, (UDINT)&"");
					FubGetIdent.Execute				= 0;
					FubGetLatestRecordID.Execute	= 0;
					FubGetPreviousRecordID.Execute	= 0;
					FubRead.Execute					= 0;
					FubReadObjectID.Execute			= 0;
					FubReadDescription.Execute		= 0;
					Step = 0;
				}
			break;
			
		}
	
		ArEventLogGetIdent(&FubGetIdent);
		ArEventLogGetLatestRecordID(&FubGetLatestRecordID);
		ArEventLogGetPreviousRecordID(&FubGetPreviousRecordID);
		ArEventLogRead(&FubRead);
		ArEventLogReadObjectID(&FubReadObjectID);
		ArEventLogReadDescription(&FubReadDescription);
		
		EdgeAssemblyErrorStop	= gAssembly.ErrorStop;

	}while(Step != StepLast);
	
	
}





void _EXIT ProgramExit(void)
{

}

